﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.Models.GoodsModels;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmGoodsQuery : Form
    {
        public FrmGoodsQuery()
        {
            InitializeComponent();
        }

        GoodsManager goodsManager = new GoodsManager();
        string selectCondition = "GoodsID";

        void TxtOpen()
        {
            txtName.Enabled = true;
            txtInPrice.Enabled = true;
            txtOutPrice.Enabled = true;
            txtInventory.Enabled = true;
            txtGoodsID.Focus();
        }

        void TxtClose()
        {
            txtName.Enabled = false;
            txtInPrice.Enabled = false;
            txtOutPrice.Enabled = false;
            txtInventory.Enabled = false;
        }

        void TxtClear()
        {
            txtGoodsID.Text = "";
            txtName.Text = "";
            txtInPrice.Text = "";
            txtOutPrice.Text = "";
            txtInventory.Text = "";
        }

        void SetHeaderText()
        {
            dgrdvGoods.Columns[0].HeaderText = "商品号";
            dgrdvGoods.Columns[0].Width = 168;
            dgrdvGoods.Columns[1].HeaderText = "商品名称";
            dgrdvGoods.Columns[1].Width = 200;
            dgrdvGoods.Columns[2].HeaderText = "进价";
            dgrdvGoods.Columns[2].Width = 100;
            dgrdvGoods.Columns[3].HeaderText = "售价";
            dgrdvGoods.Columns[3].Width = 100;
            dgrdvGoods.Columns[4].HeaderText = "库存量";
            dgrdvGoods.Columns[4].Width = 100;
        }

        void RefreshData()
        {
            List<Goods> list = new List<Goods>();
            list = goodsManager.GetGoodss();
            dgrdvGoods.DataSource = list;
            if (list.Count == 0)
            {
                TxtClear();
            }
            SetHeaderText();
        }

        private void FrmGoodsQuery_Load(object sender, EventArgs e)
        {
            try
            {
                TxtClose();
                RefreshData();
                cmbSelect.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FrmGoodsQuery_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (btnUpdate.Text == "确定")
            {
                if (MessageBox.Show("数据尚未保存，要关闭窗口吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }

        private void dgrdvGoods_CurrentCellChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgrdvGoods.CurrentCell == null)
                {
                    return;
                }
                int n = dgrdvGoods.CurrentCell.RowIndex;
                dgrdvGoods.Rows[n].Selected = true;
                txtGoodsID.Text = dgrdvGoods.Rows[n].Cells[0].Value.ToString();
                txtName.Text = dgrdvGoods.Rows[n].Cells[1].Value.ToString();
                txtInPrice.Text = dgrdvGoods.Rows[n].Cells[2].Value.ToString();
                txtOutPrice.Text = dgrdvGoods.Rows[n].Cells[3].Value.ToString();
                txtInventory.Text = dgrdvGoods.Rows[n].Cells[4].Value.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string goodsID = txtGoodsID.Text.Trim();
                if (goodsID == "")
                {
                    MessageBox.Show("没有可修改的记录", "提示");
                    return;
                }
                if (btnUpdate.Text.Trim() == "修改")
                {
                    btnUpdate.Text = "确定";
                    btnDelete.Enabled = false;
                    cmbSelect.Enabled = false;
                    txtSelect.Enabled = false;
                    btnSelect.Enabled = false;
                    btnShowAll.Enabled = false;
                    btnClose.Enabled = false;
                    txtName.Enabled = true;
                    txtInPrice.Enabled = true;
                    txtOutPrice.Enabled = true;
                    txtInventory.Enabled = true;
                    txtName.Focus();
                }
                else
                {
                    btnUpdate.Text = "修改";
                    TxtClose();
                    btnDelete.Enabled = true;
                    cmbSelect.Enabled = true;
                    txtSelect.Enabled = true;
                    btnSelect.Enabled = true;
                    btnShowAll.Enabled = true;
                    btnClose.Enabled = true;
                    if (txtName.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入商品名称", "提示");
                        txtName.Focus();
                        return;
                    }
                    if (txtInPrice.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入进货单价", "提示");
                        txtInPrice.Focus();
                        return;
                    }
                    if (txtOutPrice.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入商品售价", "提示");
                        txtOutPrice.Focus();
                        return;
                    }
                    if (txtInventory.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入库存量", "提示");
                        txtInventory.Focus();
                        return;
                    }
                    Goods goods = new Goods();
                    goods.GoodsID = txtGoodsID.Text.Trim();
                    goods.Name = txtName.Text.Trim();
                    goods.InPrice = decimal.Parse(txtInPrice.Text.Trim());
                    goods.OutPrice = decimal.Parse(txtOutPrice.Text.Trim());
                    goods.Inventory = int.Parse(txtInventory.Text.Trim());
                    if (!goodsManager.ChangeGoods(goods))
                    {
                        MessageBox.Show("修改失败", "提示");
                        return;
                    }
                    int n = dgrdvGoods.CurrentCell.RowIndex;
                    dgrdvGoods.Rows[n].Cells[1].Value = txtName.Text.Trim();
                    dgrdvGoods.Rows[n].Cells[2].Value = txtInPrice.Text.Trim();
                    dgrdvGoods.Rows[n].Cells[3].Value = txtOutPrice.Text.Trim();
                    dgrdvGoods.Rows[n].Cells[4].Value = txtInventory.Text.Trim();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string goodsID = txtGoodsID.Text.Trim();
                if (goodsID == "")
                {
                    MessageBox.Show("没有可删除的记录", "提示");
                    return;
                }
                if (MessageBox.Show("确实要删除吗？", "删除商品", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) != DialogResult.Yes)
                {
                    return;
                }
                if (!goodsManager.RemoveGoods(goodsID))
                {
                    MessageBox.Show("删除失败", "提示");
                    return;
                }
                RefreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmbSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSelect.SelectedIndex == 0)
            {
                selectCondition = "GoodsID";
            }
            else if (cmbSelect.SelectedIndex == 1)
            {
                selectCondition = "Name";
            }
            else if (cmbSelect.SelectedIndex == 2)
            {
                selectCondition = "InPrice";
            }
            else if (cmbSelect.SelectedIndex == 3)
            {
                selectCondition = "OutPrice";
            }
            else
            {
                selectCondition = "Inventory";
            }
            txtSelect.Text = "";
            txtSelect.Focus();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtSelect.Text.Trim() == "")
                {
                    MessageBox.Show("请输入查询值", "提示");
                    txtSelect.Focus();
                    return;
                }
                string selectValue = txtSelect.Text.Trim();
                List<Goods> list = new List<Goods>();
                list = goodsManager.GetGoodss(selectCondition, selectValue);
                dgrdvGoods.DataSource = list;
                if (list.Count == 0)
                {
                    TxtClear();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtSelect_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSelect_Click(sender, e);
            }
        }

        private void btnShowAll_Click(object sender, EventArgs e)
        {
            try
            {
                RefreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
